﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;

//using VBE = Microsoft.Vbe.Interop;
//using office = Microsoft.Office.Core;
using System.Data;
using System.Diagnostics;

//using 代码模块 = Microsoft.Vbe.Interop.CodeModule;
//using 代码窗格 = Microsoft.Vbe.Interop.CodePane;
//using 插件对象 = Microsoft.Vbe.Interop.AddIn;
//using 模块类型 = Microsoft.Vbe.Interop.vbext_ComponentType;
//using 菜单栏 = Microsoft.Office.Core.CommandBar;
//using 菜单栏按钮 = Microsoft.Office.Core.CommandBarButton;
//using 菜单栏控件 = Microsoft.Office.Core.CommandBarControl;
//using 菜单栏菜单 = Microsoft.Office.Core.CommandBarPopup;
//using 过程类型 = Microsoft.Vbe.Interop.vbext_ProcKind;
using VBE = NetOffice.VBIDEApi;
using office = NetOffice.OfficeApi;
using 代码模块 = NetOffice.VBIDEApi.CodeModule;
using 代码窗格 = NetOffice.VBIDEApi.CodePane;
using 插件对象 = NetOffice.VBIDEApi.AddIn;
using 模块类型 = NetOffice.VBIDEApi.Enums.vbext_ComponentType;
using 菜单栏 = NetOffice.OfficeApi.CommandBar;
using 菜单栏按钮 = NetOffice.OfficeApi.CommandBarButton;
using 菜单栏控件 = NetOffice.OfficeApi.CommandBarControl;
using 菜单栏菜单 = NetOffice.OfficeApi.CommandBarPopup;
using 过程类型 = NetOffice.VBIDEApi.Enums.vbext_ProcKind;
using static 枸杞VBE插件.VBE助手;
using 枸杞VBE插件.代码格式化;

namespace 枸杞VBE插件
{
    internal static class 扩展方法
    {
        internal static void 删除空行(this 代码模块 代码模块)
        {
            for (int i = 代码模块.CountOfLines; i > 0; i--)
            {
                if (代码模块.Lines(i, 1).Trim().Length == 0) 代码模块.DeleteLines(i, 1);
            }
        }

        internal static void 删除空行(this 代码窗格 代码窗格)
        {
            代码窗格.CodeModule.删除空行();
        }

        [Debug]
        internal static 菜单栏按钮 添加按钮(this office.CommandBarControls Controls, string 名称, int 图标ID, NetOffice.OfficeApi.CommandBarButton_ClickEventHandler 事件, string 关键字 = null)
        {
            菜单栏按钮 按钮 = (菜单栏按钮)Controls.Add(控件类型.按钮);
            按钮.Caption = 名称;
            按钮.FaceId = 图标ID;
            按钮.Style = NetOffice.OfficeApi.Enums.MsoButtonStyle.msoButtonIconAndCaption;
            按钮.Tag = "控件" + VBE助手实例.标签号++;
            按钮.ClickEvent += 事件;
            VBE助手实例.控件字典[关键字 ?? 名称] = 按钮;
            //Log.Info("添加按钮:{0}", 字典key ?? 名称);
            return 按钮;
        }

        [Debug]
        internal static 菜单栏菜单 添加菜单(this office.CommandBarControls Controls, string 名称, string 关键字 = null)
        {
            菜单栏菜单 选项菜单 = (菜单栏菜单)Controls.Add(控件类型.菜单);
            选项菜单.Caption = 名称;
            if (关键字 != null) VBE助手实例.控件字典[关键字] = 选项菜单;
            //Log.Info("添加菜单:{0}", 字典key ?? 名称);
            return 选项菜单;
        }

        internal static Selection GetSelection(this 代码窗格 代码窗格)
        {
            if (代码窗格 == null) return new Selection(0, 0, 0, 0);
            代码窗格.GetSelection(out var startLine, out var startColumn, out var endLine, out var endColumn);
            if (endLine > startLine && endColumn == 1)
            {
                endLine -= 1;
                endColumn = 代码窗格.CodeModule.Lines(endLine, 1).Length;
            }
            return new Selection(startLine, startColumn, endLine, endColumn);
        }

        internal static void SetSelection(this 代码窗格 代码窗格, Selection 选区)
        {
            代码窗格.SetSelection(选区.StartLine, 选区.StartColumn, 选区.EndLine, 选区.EndColumn);
        }

        internal static bool 取反(ref this bool b)
        {
            return b = !b;
        }

        internal static void Run(this bool b, Action action)
        {
            if (b) action();
        }

        internal static T Run<T>(this bool b, Func<T> action)
        {
            if (b) return action();
            return default;
        }

        internal static Tout Run<Tin, Tout>(this bool b, Func<Tin, Tout> action, Tin i)
        {
            if (b) return action(i);
            return default;
        }
    }
}